package com.itcoon.cloud.framework.temporal;/**
 * @author Athric Yu
 * 2024/11/15
 **/

import com.itcoon.cloud.framework.properties.TemporalPatternConfigProperties;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/15
 **/
public class LongLocalDateTimeConverter extends AbstractConverter<Long, LocalDateTime> {

    public LongLocalDateTimeConverter(TemporalPatternConfigProperties.TemporalSerialization<Long, LocalDateTime> temporalSerialization) {
        super(temporalSerialization);
    }

    @Override
    public LocalDateTime parse(Long source) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(source), ZoneOffset.of(temporalSerialization.getZoneId()));
    }

    @Override
    public Long format(LocalDateTime temporal) {
        return temporal.toInstant(ZoneOffset.of(temporalSerialization.getZoneId())).toEpochMilli();
    }
}
